package net.xdclass.product.model.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车
 */
public class CartVO {
    // 商品总件数
    private Integer count;
    // 总价格
    private BigDecimal totalAmount;
    // 实际支付价格
    private BigDecimal payAmount;
    /**
     * 购物项
     */
    private List<CartItemVO> cartItems;

    public Integer getCount() {
        if (this.cartItems != null){
            int count = this.cartItems.stream().mapToInt(CartItemVO::getCount).sum();
            return count;
        }
        return 0;
    }

    // 计算总价
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal(0);
        if (this.cartItems != null){
            for (CartItemVO cartItem : this.cartItems) {
                BigDecimal total = cartItem.getTotalAmount();
                amount = amount.add(total);
            }
            return amount;
        }
        return amount;
    }

    // 计算实际支付价格
    public BigDecimal getPayAmount() {
        BigDecimal payAmount = new BigDecimal(0);
        if (this.cartItems != null){
            for (CartItemVO cartItem : this.cartItems) {
                BigDecimal total = cartItem.getTotalAmount();
                payAmount = payAmount.add(total);
            }
            return payAmount;
        }
        return payAmount;
    }

    public List<CartItemVO> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItemVO> cartItems) {
        this.cartItems = cartItems;
    }
}
